<template>
	<div class="system-notice-container layout-pd">
		<el-card shadow="hover">
			<div class="system-notice-search mb15">
				<el-row :gutter="20">
					<el-col :span="24" v-auth="'system:notice:query'">
						<el-input size="default" placeholder="请输入公告名称" class="mt10" v-model="state.param.noticeName" clearable
							@keyup.enter.native="getTableData" />
						<el-select v-model="state.param.noticeType" placeholder="公告类型" clearable class="mt10" @change="getTableData"
							size="default">
							<el-option v-for="dict in sys_notice_type" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
						<el-button size="default" type="primary" class="mt10" @click="getTableData">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" class="mt10" type="info" @click="resetParamData()">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重置
						</el-button>
					</el-col>
					<el-col :span="24">
						<el-button size="default" type="success" class="mt10" @click="onOpenAddNotice('add')"
							v-auth="'system:notice:add'">
							<el-icon>
								<ele-Plus />
							</el-icon>
							添加
						</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="noticeId" label="公告ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="noticeTitle" label="公告标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="noticeContent" label="公告内容" show-overflow-tooltip></el-table-column>
				<el-table-column label="公告类型" align="center" prop="noticeType" width="100">
					<template #default="scope">
						<dict-tag :options="sys_notice_type" :value="scope.row.noticeType" />
					</template>
				</el-table-column>
				<el-table-column label="状态" align="center" prop="status" width="100">
					<template #default="scope">
						<dict-tag :options="sys_notice_status" :value="scope.row.status" />
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createBy" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.createTime }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="95" fixed="right" v-auths="['system:notice:edit', 'system:notice:remove']">
					<template #default="scope" >
						<el-button size="small" text type="primary" @click="onOpenEditNotice('edit', scope.row)"
							v-auth="'system:notice:edit'">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)"
							v-auth="'system:notice:remove'">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<NoticeDialog ref="noticeDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemNotice">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useNotice } from '/@/api/system/notice';
import { useDict } from '/@/utils/dict';

const { sys_notice_status, sys_notice_type } = useDict("sys_notice_status", "sys_notice_type");

// 引入组件
const NoticeDialog = defineAsyncComponent(() => import('/@/views/system/notice/dialog.vue'));

// 定义变量内容
const noticeDialogRef = ref();
const state = reactive<TableType>({
	tableData: {
		// 表格树数据
		data: [],
		// 公告树选项
		noticeOptions: [],
	},
	total: 0,
	loading: false,
	param: {
		pageNum: 1,
		pageSize: 10,
		noticeName: '',
		noticeType: '',
	},
});

/**
 * 初始化表格数据
 * @returns
 */
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const res = await useNotice().listNotice(state.param);
		state.tableData.data = res.rows;
		state.total = res.total;
	} catch (error) {
		Promise.reject(error);
	}
	state.tableData.loading = false;
};

/**
 * 打开新增公告弹窗
 * @param type
 * @param data 公告树数据
 */
const onOpenAddNotice = (type: string) => {
	noticeDialogRef.value.openDialog(type);
};
/**
 * 打开编辑公告弹窗
 * @param type
 * @param row 当前行数据
 */
const onOpenEditNotice = (type: string, row: RowNoticeType) => {
	noticeDialogRef.value.openDialog(type, row);
};

/**
 * 删除公告
 * @param row 当前行数据
 */
const onTabelRowDel = async (row: RowNoticeType) => {
	try {
		const confirmResult = await ElMessageBox.confirm(`此操作将永久删除公告：${row.noticeTitle}, 是否继续?`, '提示', {
			confirmButtonText: '删除',
			cancelButtonText: '取消',
			type: 'warning',
		});
		if (confirmResult === 'confirm') {
			const response = await useNotice().delNotice(row.noticeId);
			ElMessage.success(response.msg);
			getTableData();
		} else {
			ElMessage.info('已取消删除');
		}
	} catch (error) {
		Promise.reject(error);
	}
};



/**
 * 重置查询公告
 */
const resetParamData = () => {
	state.param = {
		pageNum: 1,
		pageSize: 10,
		noticeName: '',
		status: '',
	};
	getTableData();
};



// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style lang="scss" scoped>
.mt10 {
	max-width: 140px;
	margin: 10px 0 10px 10px;
}
</style>